#include "WallTurret.h"

#define PI 3.14f
#define GUN_ANGLE PI/6
#define R_HOUSE 30
#define TIME_FIND 500
#include "Box.h"
#include "Sprite.h"
#include "ShareComponents.h"
#include "BulletManager.h"

WallTurret::WallTurret(Box* _box) : Object(_box,ObjEnemy_STATIC)
{
	_isShooting = false;
}


WallTurret::~WallTurret(void)
{
}


void WallTurret::init()
{
	_sAngle = RSManager::getInstance()->getSprite(BLOCK_HOUSE_ANGLE);
	_sSimple = RSManager::getInstance()->getSprite(BLOCK_HOUSE_SIMPLE);
	_sStart = RSManager::getInstance()->getSprite(BLOCK_HOUSE_START);	
	_angle = PI;	
	this->delay = 0;
	this->delay2 = 0;
	_sprite = _sSimple;
	_hits=8;
	_isAlive=true;
	_obj= ObjEnemy_STATIC;

}
void WallTurret::update()
{
	delay += GAME_TIME;
	if (abs(_box->_position.x - gl_player->_position.x) > 260)
	{
		delay += GAME_TIME;
		_sprite = _sSimple;
		if (delay > 200)
		{
			delay = 0;
			_sprite->Next();
		}
		_isShooting = false;
	}
	//khi WallTurret vua ra khoi man hinh thi ngung update
	else if (_sprite == _sSimple && _box->_position.x < gl_player->_position.x)
	{
	}
	else if (_sprite == _sSimple && abs(_box->_position.x - gl_player->_position.x)<260 )
	{
		_sprite = _sStart;

	}
	else
	{
		if (_sprite == _sStart)
		{
			_sprite = new Sprite(_sAngle);
			_sprite->Reset(7);
		}
		delay2 += GAME_TIME;
		if (delay >TIME_FIND)
		{
			delay = 0;

			_angle = SetAngle(gl_player);
			int last_index = GetNumFrame(_angle);//thu tu tren sprite xet tu 0->15
			int index = _sprite->GetCurrentIndex();
			//kiem tra sprite luc dau va sprite can ve
			//co nhung truong hop ko the quay tu 15-- toi 0 ma 15 chay ngay toi 0
			if (last_index > index)
			{
				if (last_index <= 15 && last_index>=12 && index>=0 &&index<=4)
				{
					index--;
				}
				else
					index++;
			}
			else if (last_index < index)
			{
				if (last_index >=0 && last_index <=3 && index <= 15 && index>=12)
					index++;
				else
					index--;
			}
			else
				index = last_index;
			if (index<0) index = 15;
			if (index>15) index =0;
			_sprite->Reset(index);
			if (index==last_index && delay2 > 800)
			{
				index = ConvertIndex(index);
				delay2 = 0;
				_isShooting = true;
				D3DXVECTOR2 gun_direction(cosf(index*GUN_ANGLE) * R_HOUSE, sinf(index*GUN_ANGLE) * R_HOUSE);
				//_gunObject->shoot(_box->getCenterPos() + gun_direction, index*GUN_ANGLE);
				BulletManager::getInstance()->shoot(B_E,_obj,_box->getCenterPos() + gun_direction,index*GUN_ANGLE);
			}
		}
		if(_hits <= 0) _isAlive = false;
	}

}
//get suitable number of frame for every position of player
int WallTurret::GetNumFrame(float angle)
{
	//angle = pi, pi/2, 2pi, 3pi/2...
	return (int)(angle * 8 / PI) % 16;
}
//set Angle from the position of player
float WallTurret::SetAngle(Box* boxContra)
{
	float doi = boxContra->getCenterPos().y - _box->getCenterPos().y;
	float ke = boxContra->getCenterPos().x - _box->getCenterPos().x;

	return atan2(doi, ke)>0? atan2(doi, ke):2*PI +atan2(doi,ke);
}
void WallTurret::render()
{
	if(_isAlive)
	_sprite->Render(_box->_position);	
}

Box* WallTurret::getPos()
{
	return _box;
}
int WallTurret::ConvertIndex(int index)
{
	// vi chi co 12 goc nhung co toi 16 sprite
	switch (index)
	{
	case 3:
	case 4: return 3;
	case 5: return 4;
	case 6: return 5;
	case 7:
	case 8:return 6;
	case 9: return 7;
	case 10:return 8;
	case 11:
	case 12:return 9;
	case 13: return 10;
	case 14: return 11;
	case 15: return 0;
	default:
		break;
	}
}

Box* WallTurret::getCollisionBox()
{
	_colBox->_position = _box->_position + D3DXVECTOR2(15,15);
	_colBox->_size = _box->_size/2;
	_colBox->_v = _box->_v;
	return _colBox;
}
